package com.pet.serve.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;

@Service
public class FileService {

    @Value("${file.upload-dir}")
    private static String uploadDir;

    //不知道为什么存在的防呆设计，用于创建上传文件夹
    public static void createUploadDirectory() {
        File directory = new File(uploadDir);
        if (!directory.exists()) {
            directory.mkdirs();  // 创建目录及其父目录
        }
    }

    public static String save(MultipartFile file) throws IOException {
        createUploadDirectory(); // 确保目录存在
        String fileName = file.getOriginalFilename();
        File destinationFile = new File(uploadDir + File.separator + fileName);
        file.transferTo(destinationFile);
        return fileName;
    }


}
